//2->10
var b2i = function(b){
var strB = b.toString();
var sum = 0;
for(var i = 0; i<strB.length ;i++){
sum += Math.pow(2,strB.length - i-1) * strB[i];
}
return sum;
}
//10 -> 2
var r = "";
var i2b = function f(i){
if(i ==0 || i == 1){ r= i + r; return;}
if(i%2 == 0){r="0"+r;}
else{r="1" + r;}
f( i/2 | 0 );
}
//2->10 test
console.log(b2i(1001));
//10->2 test
i2b(18);
console.log(r);
算法练习--十进制 二进制互转
最新推荐文章于 2024-07-24 08:15:58 发布